package com.automannn.atm_back.modules.codeGenerator.handler;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.TypeMapperEntity;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chenkh
 * @time 2020/8/4 8:49
 */
@Component
public class DefaultMapperHandler implements MapperHandler{

    @Override
    public void handle(ColumnConfigEntity columnConfigEntity, Collection<TypeMapperEntity> typeMapperCollection) {
        AtomicBoolean findFlag = new AtomicBoolean(false);
        typeMapperCollection.stream().forEach(item->{
           String shortType = columnConfigEntity.getShortType();
           Pattern pattern = Pattern.compile(item.getColumnType());
            Matcher matcher= pattern.matcher(shortType);
           if (matcher.find()){
               columnConfigEntity.setType(item.getJavaType());
               findFlag.set(true);
           }
        });
        if(!findFlag.get()){
            throw new RuntimeException("属性类型:"+columnConfigEntity.getShortType()+"未找到映射类型!");
        }
    }
}
